package com.cn.admin.controller;

import com.cn.admin.method.GrabOrders;
import com.cn.admin.unit.Tool;
import com.cn.admin.vo.order.OrderVo;
import com.cn.api.OrderCoreService;
import com.cn.common.result.Result;
import com.cn.dto.OrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("GrabOrders")
@RestController
public class GrabOrdersController implements GrabOrders {
    @Resource
    OrderCoreService orderCoreService;
    @Override
    @PostMapping("GrabOrders")
    public Result GrabOrders(int adminId, String orderNumber) {
        OrderDTO lock = orderCoreService.lock(adminId, orderNumber);
        OrderVo orderVo = Tool.ODtoToVO(lock);
        if (orderVo!=null){
            OrderDTO byOrderNumber = orderCoreService.findByOrderNumber(orderNumber);
            OrderVo orderVo2 = Tool.ODtoToVO(byOrderNumber);
            return Result.success(orderVo2,"请及时处理订单");
        }
       else {
           return Result.failed("该订单正在处理中");
        }
    }

    @Override
    @PostMapping("success")
    public Result success(int adminId, String orderNumber) {
        int success = orderCoreService.success(adminId, orderNumber);
        return Result.success(success,"出票成功");
    }

    @Override
    @PostMapping("fail")
    public Result fail(String orderNumber) {
        int fail = orderCoreService.fail(orderNumber);
        return Result.success(fail,"提交成功");
    }
}
